package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.es.ApAssociateWordsRepository;
import com.heima.search.service.IApAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author WSJ
 * @Date 2022-03-26-21:35
 * @Version 1.0 （版本号）
 */
@Service
public class ApAssociateWordsServiceImpl implements IApAssociateWordsService {
    @Autowired
    private ApAssociateWordsRepository repository;

    @Override
    public ResponseResult search(ApArticleSearchDto dto) {
        //如果传入为空，返回空集合
        if (StringUtils.isEmpty(dto.getSearchWords().trim())) {
            return ResponseResult.okResult(new ArrayList<>());
        }
        //查询联想词
        List<ApAssociateWords> wordsList = repository.findTop10ByAssociateWordsContainsOrderByCreatedTimeDesc(dto.getSearchWords());
        return ResponseResult.okResult(wordsList);
    }
}
